// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   UserDAO.java

package com.nervytech.tutorcruise.db.dao.mysql.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

import com.nervytech.tutorcruise.db.dao.interfaces.IUserDAO;
import com.nervytech.tutorcruise.db.dao.model.User;

@Repository("userDAO")
public class UserDAO extends JdbcDaoSupport implements IUserDAO {

	@Autowired
	UserDAO(DataSource dataSource) {
		setDataSource(dataSource);
	}

	public void saveUser(User user) {
		Map argsMap = new HashMap();
		argsMap.put("email", user.getEmail());
		argsMap.put("full_name", user.getFullName());
		argsMap.put("password", user.getPassword());
		argsMap.put("user_type", Integer.valueOf(user.getUserType()));
		argsMap.put("user_status", Integer.valueOf(user.getUserStatus()));
		argsMap.put("zip_code", user.getZipCode());
		argsMap.put("created_time", user.getCreatedTime());
		argsMap.put("updated_time", user.getUpdatedTime());
		long userId = (new SimpleJdbcInsert(getJdbcTemplate()))
				.withTableName("user")
				.usingGeneratedKeyColumns(new String[] { "user_id" })
				.executeAndReturnKey(argsMap).longValue();
		user.setUserId(userId);
	}

	public User getUser(long userId) {
		return null;
	}

	public void updateUser(User user1) {
	}

	public boolean checkIfUserExist(String emailId) {
		Object parms[] = { emailId };
		try {
			// getJdbcTemplate().queryForObject("select user_id from user where user.email = ?",
			// parms, java/lang/Long);
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	public List getUserByEmailId(final String emailId) {
		RowMapper mapper = new RowMapper() {

			public User mapRow(ResultSet rs, int rowNum) throws SQLException {
				User usr = new User();
				usr.setUserId(rs.getInt("user_id"));
				usr.setFullName(rs.getString("full_name"));
				usr.setEmail(emailId);
				usr.setUserStatus(rs.getInt("user_status"));
				usr.setUserType(rs.getInt("user_type"));
				usr.setZipCode(rs.getString("zip_code"));
				usr.setCity(rs.getInt("city"));
				usr.setCategory(rs.getInt("category"));
				usr.setContactNumber(rs.getString("contact_number"));
				usr.setLastLoginTime(rs.getDate("last_login_time"));
				usr.setPassword(rs.getString("password"));
				return usr;
			}

		};
		String selectUserQuery = (new StringBuilder(
				"SELECT * FROM USER WHERE email='")).append(emailId)
				.append("'").toString();
		logger.debug((new StringBuilder("Select user Query : ")).append(
				selectUserQuery).toString());
		return getJdbcTemplate().query(selectUserQuery, mapper);
	}

	public void updatePassword(long userId, String password) {
		String saveUserQuery = "update user set password=? where user_id=?";
		Object parms[] = { password, Long.valueOf(userId) };
		getJdbcTemplate().update(saveUserQuery, parms);
	}

	public void updateUserStatus(long userId, int userStatus) {
		String saveUserQuery = "update user set user_status=? where user_id=?";
		Object parms[] = { Integer.valueOf(userStatus), Long.valueOf(userId) };
		getJdbcTemplate().update(saveUserQuery, parms);
	}

	public void updateUserTypeAndCategory(long userId, int userType,
			int category) {
		String saveUserQuery = "update user set user_type=?, category=? where user_id=?";
		Object parms[] = { Integer.valueOf(userType),
				Integer.valueOf(category), Long.valueOf(userId) };
		getJdbcTemplate().update(saveUserQuery, parms);
	}

	public void updateFirstLogin(long userId, int userType, int category,
			String mobile, int mobileVerfied) {
		String saveUserQuery = "update user set user_type=?, category=?, contact_number=?, mobile_verified=? where user_id=?";
		Object parms[] = { Integer.valueOf(userType),
				Integer.valueOf(category), mobile,
				Integer.valueOf(mobileVerfied), Long.valueOf(userId) };
		getJdbcTemplate().update(saveUserQuery, parms);
	}

	public List getTraineeProfile(long userId) {
		RowMapper mapper = new RowMapper() {

			public User mapRow(ResultSet rs, int rowNum) throws SQLException {
				User usr = new User();
				usr.setUserId(rs.getInt("user_id"));
				usr.setFullName(rs.getString("full_name"));
				usr.setEmail(rs.getString("email"));
				usr.setUserStatus(rs.getInt("user_status"));
				usr.setUserType(rs.getInt("user_type"));
				usr.setZipCode(rs.getString("zip_code"));
				usr.setCity(rs.getInt("city"));
				usr.setCategory(rs.getInt("category"));
				usr.setContactNumber(rs.getString("contact_number"));
				usr.setLastLoginTime(rs.getDate("last_login_time"));
				usr.setPassword(rs.getString("password"));
				return usr;
			}

		};
		String selectUserQuery = (new StringBuilder(
				"SELECT * FROM USER WHERE user_id='")).append(userId)
				.append("'").toString();
		logger.debug((new StringBuilder("Select user Query : ")).append(
				selectUserQuery).toString());
		return getJdbcTemplate().query(selectUserQuery, mapper);
	}

	private static final String EMAIL_CHECK_QUERY = "select user_id from user where user.email = ?";
}
